﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using MvcExtensions.Controls;

namespace MvcExtensions.Extensions
{
    public static class DynamicGridExtensions
    {
        public static MvcHtmlString GridFor<TModel, TValue>(this HtmlHelper<IEnumerable<TModel>> html,
             Expression<Func<TModel, TValue>> expression,
             IEnumerable<DynamicGridColumn> columns = null)
        {
            var grid = new DynamicGrid<TModel>(html.ViewData.Model)
            {
                Columns = columns,
                HtmlHelper = html
            };
            return grid.ToHtml();
        }

        public static MvcHtmlString GridFor1<TModel, TValue>(this HtmlHelper<TModel> html,
                                                            Expression<Func<TModel, IEnumerable<TValue>>> expression,
                                                            IEnumerable<DynamicGridColumn> columns = null)
        {
            var modelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);

            var grid = new DynamicGrid<TValue>(modelMetadata.Model as IEnumerable<TValue>)
            {
                Columns = columns,
                //HtmlHelper = html
            };
            return grid.ToHtml();
        }

        public static DynamicGridColumn<TModel, TValue> GridColumnFor<TModel, TValue>(this HtmlHelper<IEnumerable<TModel>> html,
            Expression<Func<TModel, TValue>> expression, 
            string title = null,
            Func<TModel, string> format = null)
        {
            title = title ?? html.DisplayNameFor(expression).ToString();

            return new DynamicGridColumn<TModel, TValue>(title, title)
            {
                Expression = expression,
                Format = format
            };
        }

        public static DynamicGridColumn<TModel, TValue> GridTextColumnFor<TModel, TValue>(this HtmlHelper<IEnumerable<TModel>> html,
            Expression<Func<TModel, TValue>> expression, 
            string title = null,
            Func<TModel, string> format = null)
        {
            title = title ?? html.DisplayNameFor(expression).ToString();

            return new DynamicGridTextColumn<TModel, TValue>(title, title)
            {
                Expression = expression,
                Format = format
            };
        }

        public static DynamicGridColumn<TModel, TValue> GridCheckboxColumnFor<TModel, TValue>(this HtmlHelper<IEnumerable<TModel>> html,
            Expression<Func<TModel, TValue>> expression,
            string title = null,
            Func<TModel, string> format = null)
        {
            title = title ?? html.DisplayNameFor(expression).ToString();

            return new DynamicGridCheckboxColumn<TModel, TValue>(title, title)
            {
                Expression = expression,
                Format = format
            };
        }
        
    }
}
